package com.jht.bletool2java.characteristic.heartrateservice;


import android.bluetooth.BluetoothGattCharacteristic;
import android.util.Log;

import androidx.annotation.Keep;


import com.jht.bletool2java.characteristic.TranslateData;
import com.jht.bletool2java.util.ByteUtil;

import top.codestudy.annotation_uuid.MyUUID;

@Keep
@MyUUID(uuid="00002a37-0000-1000-8000-00805f9b34fb")
public class HeartRateMeasurement implements TranslateData {
    private static final String TAG = "HeartRateMeasurement";
    private int heart;
    private byte[] value;
    private boolean isInvalidData = false;
    public HeartRateMeasurement() {
    }

    public HeartRateMeasurement(BluetoothGattCharacteristic characteristic) {
        int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;

        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;

        }
        value = characteristic.getValue();
        if (format == BluetoothGattCharacteristic.FORMAT_UINT16){
            Log.i(TAG, "HeartRateMeasurement: heartRate ==> " + ByteUtil.bytes2ToInt(value,1));
        }
        if (format == BluetoothGattCharacteristic.FORMAT_UINT8){
            Log.i(TAG, "HeartRateMeasurement: heartRate ==> " + ByteUtil.byte1ToInt(value,1));
        }

        this.heart = characteristic.getIntValue(format, 1);
    }

    @Override
    public String convert2String() {
        return "heart rate is " + heart + " bpm"+"\n";
    }

    @Override
    public String merge(TranslateData translateData) {
        return null;
    }
    @Override
    public boolean hasMoreData() {
        return false;
    }

    @Override
    public byte[] getData() {
        return value;
    }

    @Override
    public boolean isInvalidData() {
        return isInvalidData;
    }

    @Override
    public void parseData(BluetoothGattCharacteristic characteristic, byte[] value) {

        if (characteristic == null){
            //非法数据
            this.value =value;
            this.heart = 0;
            isInvalidData = true;
            Log.w(TAG, "parseData: characteristic = null");
        }else {
            int flag = characteristic.getProperties();
            int format;
            if ((flag & 0x01) != 0) {
                format = BluetoothGattCharacteristic.FORMAT_UINT16;

            } else {
                format = BluetoothGattCharacteristic.FORMAT_UINT8;

            }
            this.value = value;
            isInvalidData = value == null;
            if (isInvalidData){
                this.heart = 0;
            }else {
                if (format == BluetoothGattCharacteristic.FORMAT_UINT16){
                    Log.d(TAG, "HeartRateMeasurement: heartRate ==> " + ByteUtil.bytes2ToInt(value,1));
                    if (value.length < 2){
                        isInvalidData = true;
                        this.heart = 0;
                    }else {
                        this.heart = characteristic.getIntValue(format, 1);
                    }

                }
                if (format == BluetoothGattCharacteristic.FORMAT_UINT8){
                    Log.d(TAG, "HeartRateMeasurement: heartRate ==> " + ByteUtil.byte1ToInt(value,1));
                    if (value.length < 1){
                        isInvalidData = true;
                        this.heart = 0;
                    }else {
                        this.heart = characteristic.getIntValue(format, 1);
                    }

                }
            }

        }


    }
}
